package com.wangxiaonan.wxwork.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.wxwork.exeption.ServiceException;
import com.wangxiaonan.wxwork.service.UserService;
import com.wangxiaonan.wxwork.util.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public JSONObject getUser(String token, String userId) {
        if (StringUtils.isBlank(token) || StringUtils.isBlank(userId)) {
            logger.error("[UserServiceImpl] getUser >> token: {}, userId: {}", token, userId);
            throw new ServiceException("param_invalid", "请求参数不能为空");
        }
        String result;
        try {
            String url = String.format(UserService.GET_USER_GET_URL, token, userId);
            result = HttpUtil.get(url);
        } catch (IOException e) {
            throw new ServiceException("connect_refuse", "接口请求失败");
        }
        JSONObject json;
        try {
            json = JSONObject.parseObject(result);
        } catch (Exception e) {
            logger.error("[UserServiceImpl] getUser >> result: {}", result);
            throw new ServiceException("object_format_error", "请求结果格式化错误");
        }

        return json;
    }

    @Override
    public JSONObject updateUser(String token, String param) {
        if (StringUtils.isBlank(token)) {
            logger.error("[UserServiceImpl] updateUser >> token: {}", token);
            throw new ServiceException("param_invalid", "请求参数不能为空");
        }
        String result;
        try {
            String url = String.format(UserService.POST_USER_UPDATE_URL, token);
            result = HttpUtil.post(url, param);
        } catch (IOException e) {
            throw new ServiceException("connect_refuse", "接口请求失败");
        }
        JSONObject json;
        try {
            json = JSONObject.parseObject(result);
        } catch (Exception e) {
            logger.error("[UserServiceImpl] updateUser >> result: {}", result);
            throw new ServiceException("object_format_error", "请求结果格式化错误");
        }

        return json;
    }

    @Override
    public JSONObject getUserList(String token, String deptId, String isFetchChild) {
        if (StringUtils.isBlank(token) || StringUtils.isBlank(deptId) || StringUtils.isBlank(isFetchChild)) {
            logger.error("[UserServiceImpl] getUser >> token: {}, deptId: {}, isFetchChild: {}", token, deptId, isFetchChild);
            throw new ServiceException("param_invalid", "请求参数不能为空");
        }

        String result;
        try {
            String url = String.format(UserService.GET_USER_SIMPLE_LIST_URL, token, deptId, isFetchChild);
            result = HttpUtil.get(url);
        } catch (IOException e) {
            throw new ServiceException("connect_refuse", "接口请求失败");
        }
        JSONObject json;
        try {
            json = JSONObject.parseObject(result);
        } catch (Exception e) {
            logger.error("[UserServiceImpl] getUserList >> result: {}", result);
            throw new ServiceException("object_format_error", "请求结果格式化错误");
        }

        return json;
    }

    @Override
    public JSONObject getUserInfoList(String token, String deptId, String isFetchChild) {
        if (StringUtils.isBlank(token) || StringUtils.isBlank(deptId) || StringUtils.isBlank(isFetchChild)) {
            logger.error("[UserServiceImpl] getUser >> token: {}, deptId: {}, isFetchChild: {}", token, deptId, isFetchChild);
            throw new ServiceException("param_invalid", "请求参数不能为空");
        }
        String result;
        try {
            String url = String.format(UserService.GET_USER_LIST_URL, token, deptId, isFetchChild);
            result = HttpUtil.get(url);
        } catch (IOException e) {
            throw new ServiceException("connect_refuse", "接口请求失败");
        }
        JSONObject json;
        try {
            json = JSONObject.parseObject(result);
        } catch (Exception e) {
            logger.error("[UserServiceImpl] getUserList >> result: {}", result);
            throw new ServiceException("object_format_error", "请求结果格式化错误");
        }

        return json;
    }
}
